﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VietThuongLMS.ViewModels;
using System.Web.Mvc;
using System.Data.Entity;
//Author : Kha
namespace VietThuongLMS.Services
{
    public class LoaiLopHoc_GiaoVienService
    {
        public LoaiLopHoc_GiaoVienViewModel GetBangLuongByIDTaiKhoanGiaoVienWithPagination(int id, int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {
                var llh_gvmrm = (from tk in context.TaiKhoans
                           from gv in context.GiaoViens
                           from llh_gv in context.LoaiLopHoc_GiaoVien
                           where tk.ID == id && tk.ID == gv.MaTaiKhoan && gv.ID == llh_gv.MaGiaoVien
                           select new LoaiLopHoc_GiaoVienMoRongModel { LLH_GV=llh_gv, TenLoaiLopHoc=llh_gv.LoaiLopHoc.TenLoaiLopHoc, TenGiaoVien=tk.HoTen })
                           .OrderBy(p => p.LLH_GV.MaLoaiLopHoc).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = (from tk in context.TaiKhoans
                             from gv in context.GiaoViens
                             from llh_gv in context.LoaiLopHoc_GiaoVien
                             where tk.ID == id && tk.ID == gv.MaTaiKhoan && gv.ID == llh_gv.MaGiaoVien
                             select new LoaiLopHoc_GiaoVienMoRongModel { LLH_GV = llh_gv, TenLoaiLopHoc = llh_gv.LoaiLopHoc.TenLoaiLopHoc, TenGiaoVien = tk.HoTen }).Count();
                LoaiLopHoc_GiaoVienViewModel llh_gvvm = new LoaiLopHoc_GiaoVienViewModel(llh_gvmrm,total);
                return llh_gvvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiLopHoc_GiaoVienService::GetBangLuongByIDTaiKhoanGiaoVien", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public LoaiLopHoc_GiaoVienViewModel GetBangLuongByMaGiaoVienWithPagination(int mgv, int pageNum, int pageIndex)
        {
            var context = new LMSDataEntities();
            try
            {
                var llh_gvmrm = (from gv in context.GiaoViens
                                 from llh_gv in context.LoaiLopHoc_GiaoVien
                                 where gv.ID == mgv && gv.ID == llh_gv.MaGiaoVien
                                 select new LoaiLopHoc_GiaoVienMoRongModel { LLH_GV = llh_gv, TenLoaiLopHoc = llh_gv.LoaiLopHoc.TenLoaiLopHoc, TenGiaoVien = gv.TaiKhoan.HoTen })
                           .OrderBy(p => p.LLH_GV.MaLoaiLopHoc).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
                int total = (from gv in context.GiaoViens
                             from llh_gv in context.LoaiLopHoc_GiaoVien
                             where gv.ID == mgv && gv.ID == llh_gv.MaGiaoVien
                             select new LoaiLopHoc_GiaoVienMoRongModel { LLH_GV = llh_gv, TenLoaiLopHoc = llh_gv.LoaiLopHoc.TenLoaiLopHoc, TenGiaoVien = gv.TaiKhoan.HoTen }).Count();
                LoaiLopHoc_GiaoVienViewModel llh_gvvm = new LoaiLopHoc_GiaoVienViewModel(llh_gvmrm, total);
                return llh_gvvm;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("LoaiLopHoc_GiaoVienService::GetBangLuongByIDTaiKhoanGiaoVien", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        //public static BangLuongViewModel GetAllWithPagination(int pageNum, int pageIndex)
        //{
        //    var context = new LMSDataEntities();
        //    try
        //    {
        //        var lbl = (from p in context.BangLuongs select new BangLuongMoRongModel{BL= p, TenLoaiLopHoc= p.LoaiLopHoc.TenLoaiLopHoc, 
        //                    HocPhiLopHoc=p.LoaiLopHoc.HocPhi }).OrderBy(bl => bl.BL.NgayBatDau).Skip(pageNum * (pageIndex - 1)).Take(pageNum).ToList();
        //        int total = context.BangLuongs.Count();
        //        return new BangLuongViewModel(lbl, total);
        //    }
        //    catch (Exception ex)
        //    {
        //        DCS.ComLib.File.Log.Write("SalaryServices::GetAllWithPagination", ex.StackTrace, ex.Message);
        //        throw ex;
        //    }
        //    finally
        //    {
        //        context.Dispose();
        //    }
        //}
        public LoaiLopHoc_GiaoVien GetLoaiLopHocGiaoVienByID(int id)
        {
            var context = new LMSDataEntities();
            try
            {
                var llh_gv = (from c in context.LoaiLopHoc_GiaoVien where c.ID == id select c).First();
                return llh_gv;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("SalaryServices::GetLoaiLopHocGiaoVienByID", ex.StackTrace, ex.Message);
                throw ex;
            }
            finally
            {
                context.Dispose();
            }
        }
        public Boolean UpdateLoaiLopHoc_GiaoVien(LoaiLopHoc_GiaoVien llh_gv)
        {
            var context = new LMSDataEntities();

            try
            {
                var llh_gvmk = context.LoaiLopHoc_GiaoVien.Where(p => p.ID == llh_gv.ID).FirstOrDefault();
                llh_gvmk.Luong = llh_gv.Luong;
                llh_gvmk.MaLoaiLopHoc = llh_gv.MaLoaiLopHoc;
                llh_gvmk.NgayBatDau = llh_gv.NgayBatDau;
                llh_gvmk.NgayKetThuc = llh_gv.NgayKetThuc;
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("SalaryServices::UpdateLoaiLopHoc_GiaoVien", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        public Boolean DeleteOneLoaiLopHoc_GiaoVien(int id)
        {
            var context = new LMSDataEntities();

            try
            {
                var blmk = context.LoaiLopHoc_GiaoVien.Where(p => p.ID == id).FirstOrDefault();
                context.LoaiLopHoc_GiaoVien.DeleteObject(blmk);
                context.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("SalaryServices::DeleteOneLoaiLopHoc_GiaoVien", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
        public Boolean Insert(LoaiLopHoc_GiaoVien llh_gv)
        {
            LMSDataEntities context = new LMSDataEntities();
            try
            {
                var newllh_gv = new LoaiLopHoc_GiaoVien();
                newllh_gv.MaGiaoVien = llh_gv.MaGiaoVien;
                newllh_gv.MaLoaiLopHoc = llh_gv.MaLoaiLopHoc;
                newllh_gv.NgayBatDau = Convert.ToDateTime(llh_gv.NgayBatDau);
                newllh_gv.NgayKetThuc = Convert.ToDateTime(llh_gv.NgayKetThuc);
                newllh_gv.Luong = llh_gv.Luong;
                context.AddToLoaiLopHoc_GiaoVien(newllh_gv);
                context.SaveChanges();
                return true;

            }
            catch (Exception ex)
            {
                DCS.ComLib.File.Log.Write("SalaryServices::Insert", ex.StackTrace, ex.Message);
                return false;
            }
            finally
            {
                context.Dispose();
            }
        }
    }
}